package kr.co.doriclassic.chapter22;

import java.util.Arrays;

public class Array {
	public static  <T> T getLast(T[] a){
		return a[a.length-1];
	}
	
	public static void main(String[] args) {
		String[] language = {"Java", "C#", "C++", "Basic"};
		Integer[] arr = {5,4,3,2,1};//래퍼클래스만 가능
		
		String last = Array.getLast(language);
		//String last = Array.<String>getLast(language);
		System.out.println(last);
		
		int aLast = Array.getLast(arr);
		System.out.println(aLast);
		
		//int aMax = Array.getMax(arr);
		
		Test[] arrTest = new Test[3];
		arrTest[0] = new Test(5);
		arrTest[1] = new Test(4);
		arrTest[2] = new Test(3);
		System.out.println(Arrays.toString(arr)+"중에사 가장 큰 값은"+Array.getMax(arr));
		System.out.println(Arrays.toString(arr)+"중에서 가장 작은 값은"+Array.getMin(arr));
	
		
		
	}
	
	public static <T extends Comparable<T>> T getMax(T[] a){
		if(a==null||a.length==0) return null;
		
		T largest = a[0];
		
		for (int i = 0; i < a.length; i++) {
			if(largest.compareTo(a[i])<0){
				largest = a[i];
			}
		}
		return largest;
		
	}
	
	public static <T extends Comparable<T>> T getMin(T[] a){
		if(a==null || a.length==0){
			return null;
		}
		T smallest = a[0];
		
		for (int i = 0; i < a.length; i++) {
			if(smallest.compareTo(a[i])>0){
				smallest = a[i];
			}
		}
		return smallest;		
		
	}
}

class Test implements Comparable<Test>{
	private int a;

	public Test(int a) {
		this.a = a;
	}

	public int getA() {
		return a;
	}

	public void setA(int a) {
		this.a = a;
	}

	
	@Override
	public int compareTo(Test o) {
		return a-o.a;
	}
	
}